import request from '../utils/request'

// 知识空间相关API
export const knowledgeSpaceApi = {
  // 获取空间列表
  getSpaces(username) {
    return request({
      method: 'GET',
      url: '/api/knowledge/spaces',
      params: { username }
    })
  },

  // 获取空间详情
  getSpaceById(spaceId, username) {
    return request({
      method: 'GET',
      url: `/api/knowledge/spaces/${spaceId}`,
      params: { username }
    })
  },

  // 创建空间
  createSpace(spaceData, username) {
    console.log('API调用 - 空间数据:', spaceData)
    console.log('API调用 - 用户名参数:', username)
    console.log('API调用 - 完整URL:', '/api/knowledge/spaces?username=' + encodeURIComponent(username))
    return request({
      method: 'POST',
      url: '/api/knowledge/spaces',
      data: spaceData,
      params: { username }
    })
  },

  // 更新空间
  updateSpace(spaceId, spaceData, username) {
    return request({
      method: 'PUT',
      url: `/api/knowledge/spaces/${spaceId}`,
      data: spaceData,
      params: { username }
    })
  },

  // 删除空间
  deleteSpace(spaceId, username) {
    return request({
      method: 'DELETE',
      url: `/api/knowledge/spaces/${spaceId}`,
      params: { username }
    })
  },

  // 检查空间权限
  checkPermission(spaceId, username) {
    return request({
      method: 'GET',
      url: `/api/knowledge/spaces/${spaceId}/permission`,
      params: { username }
    })
  }
}

// 文档相关API
export const documentApi = {
  // 获取空间下的文档列表
  getDocumentsBySpace(spaceId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/space/${spaceId}`,
      params: { username }
    })
  },

  // 获取文档详情
  getDocumentById(documentId, username, fromRecentOrFavorite = false) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}`,
      params: { 
        username,
        fromRecentOrFavorite 
      }
    })
  },

  // 创建文档
  createDocument(documentData, username) {
    return request({
      method: 'POST',
      url: '/api/documents',
      data: documentData,
      params: { username }
    })
  },

  // 更新文档
  updateDocument(documentId, documentData, username) {
    return request({
      method: 'PUT',
      url: `/api/documents/${documentId}`,
      data: documentData,
      params: { username }
    })
  },

  // 删除文档
  deleteDocument(documentId, username) {
    return request({
      method: 'DELETE',
      url: `/api/documents/${documentId}`,
      params: { username }
    })
  },

  // 获取收藏文档
  getFavoriteDocuments(username) {
    return request({
      method: 'GET',
      url: '/api/documents/favorites',
      params: { username }
    })
  },

  // 添加收藏
  addToFavorites(documentId, username) {
    return request({
      method: 'POST',
      url: `/api/documents/${documentId}/favorite`,
      params: { username }
    })
  },

  // 取消收藏
  removeFromFavorites(documentId, username) {
    return request({
      method: 'DELETE',
      url: `/api/documents/${documentId}/favorite`,
      params: { username }
    })
  },

  // 获取最近访问文档
  getRecentDocuments(username, limit = 10) {
    return request({
      url: '/api/documents/recent',
      method: 'GET',
      params: { username, limit }
    })
  },

  // 上传文档文件
  uploadDocument(file, spaceId, username, parentFolderId = null, description = '') {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('spaceId', spaceId)
    formData.append('username', username)
    if (parentFolderId) {
      formData.append('parentFolderId', parentFolderId)
    }
    if (description) {
      formData.append('description', description)
    }
    
    return request({
      method: 'POST',
      url: '/api/upload/document',
      data: formData
    })
  },

  // 搜索文档
  searchDocuments(keyword, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search',
      params: { keyword, spaceId, username }
    })
  },

  // 按标题搜索文档
  searchDocumentsByTitle(title, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search/title',
      params: { title, spaceId, username }
    })
  },

  // 按内容搜索文档
  searchDocumentsByContent(content, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search/content',
      params: { content, spaceId, username }
    })
  },

  // 按创建者搜索文档
  searchDocumentsByCreator(creatorUsername, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search/creator',
      params: { creatorUsername, spaceId, username }
    })
  },

  // 按类型搜索文档
  searchDocumentsByType(documentType, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search/type',
      params: { documentType, spaceId, username }
    })
  },

  // 高级搜索文档
  advancedSearchDocuments(params) {
    return request({
      method: 'GET',
      url: '/api/documents/search/advanced',
      params
    })
  },

  // 获取搜索建议
  getSearchSuggestions(keyword, spaceId, username) {
    return request({
      method: 'GET',
      url: '/api/documents/search/suggestions',
      params: { keyword, spaceId, username }
    })
  },

  // 根据文件夹ID获取文档
  getDocumentsByFolderId(folderId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/folder/${folderId}`,
      params: { username }
    })
  },

  // 开始编辑会话
  startEditSession(documentId, username) {
    return request({
      method: 'POST',
      url: `/api/documents/${documentId}/edit/start`,
      params: { username }
    })
  },

  // 结束编辑会话
  endEditSession(sessionId, username) {
    return request({
      method: 'POST',
      url: `/api/documents/edit/end/${sessionId}`,
      params: { username }
    })
  },

  // 保存文档版本
  saveDocumentWithVersion(documentId, content, username, editSummary) {
    return request({
      method: 'POST',
      url: `/api/documents/${documentId}/save`,
      data: { content, editSummary },
      params: { username }
    })
  },

  // 自动保存草稿
  autoSaveDraft(documentId, content, username, sessionId) {
    return request({
      method: 'POST',
      url: `/api/documents/${documentId}/draft`,
      data: { content, sessionId },
      params: { username }
    })
  },

  // 获取文档版本历史
  getDocumentVersions(documentId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}/versions`,
      params: { username }
    })
  },

  // 获取文档内容
  getDocumentContent(documentId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}/content`,
      params: { username }
    })
  },

  // 获取Markdown预览
  getMarkdownPreview(documentId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}/preview`,
      params: { username }
    })
  },

  // 生成Markdown目录
  generateMarkdownToc(documentId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}/toc`,
      params: { username }
    })
  },

  // 提取Markdown资源
  extractMarkdownResources(documentId, username) {
    return request({
      method: 'GET',
      url: `/api/documents/${documentId}/resources`,
      params: { username }
    })
  }
}

// 文档文件夹相关API
export const documentFolderApi = {
  // 获取空间下的文件夹列表
  getFoldersBySpace(spaceId, username) {
    return request({
      method: 'GET',
      url: `/api/folders/space/${spaceId}`,
      params: { username }
    })
  },

  // 获取文件夹详情
  getFolderById(folderId, username) {
    return request({
      method: 'GET',
      url: `/api/folders/${folderId}`,
      params: { username }
    })
  },

  // 创建文件夹
  createFolder(folderData, username) {
    return request({
      method: 'POST',
      url: '/api/folders',
      data: folderData,
      params: { username }
    })
  },

  // 更新文件夹
  updateFolder(folderId, folderData, username) {
    return request({
      method: 'PUT',
      url: `/api/folders/${folderId}`,
      data: folderData,
      params: { username }
    })
  },

  // 删除文件夹
  deleteFolder(folderId, username) {
    return request({
      method: 'DELETE',
      url: `/api/folders/${folderId}`,
      params: { username }
    })
  },

  // 获取文件夹树结构
  getFolderTree(spaceId, username) {
    return request({
      method: 'GET',
      url: `/api/folders/space/${spaceId}/tree`,
      params: { username }
    })
  }
}

// 导出所有API
export default {
  knowledgeSpaceApi,
  documentApi,
  documentFolderApi
}